from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
# 输入用户名，密码
username=input('手机号')
password=input('密码')
# username = "15086801185"
# password = "Lzf123456"
# 创建设置项
options = webdriver.EdgeOptions()
# 无头模式
# options.add_argument('--headless')

# 禁用GPU，防止无头模式出现莫名的BUG
options.add_argument('--disable-gpu')

# 修改设置使程序结束后浏览器不自动关闭
options.add_experimental_option('detach', True)
# 设置驱动位置


s = Service("msedgedriver.exe")
# 填写webdriver的保存目录
try:
    driver = webdriver.Edge(options=options,service=s)
except:
    input("驱动版本不匹配！")

# 打开网址
url = 'https://www.cqooc.com/learn/mooc/structure?id=334572167'
driver.get(url)

start_time = time.time()
time.sleep(0.5)
signtype = driver.find_element(By.XPATH, '//span[@class="sign-type"]')
users = driver.find_element(By.XPATH, '//input[@class="username"]')
passwd = driver.find_element(By.XPATH, '//input[@class="password"]')
loginBtn = driver.find_element(By.XPATH, '//span[@id="loginBtn"]')
signtype.click()
# 一个一个输入
username_list = list(username)
password_list = list(password)
users.click()
for i in username_list:
    users.send_keys(i)
    # time.sleep(1)
passwd.click()
for i in password_list:
    passwd.send_keys(i)
    # time.sleep(1)
loginBtn.click()
# 页面加载时间
time.sleep(5)

video_class = driver.find_elements(By.XPATH, '//li[contains(@class,"one") and contains(@class,"close")]/a')
for item in video_class:
    # 展开所有大类
    if item.get_attribute('innerHTML')[0:2]=='模块':
        ActionChains(driver).move_to_element(item).click(item).perform()
# 获得所有视频链接
video_list = []
qwer = driver.find_elements(By.XPATH, '//li[contains(@class,"item") and contains(@class,"s0")]/a')
for i in qwer:
    if i.get_attribute('innerHTML')[0:2] == '视频':
        video_list.append(i)

for index, video_name in enumerate(video_list):
    ActionChains(driver).move_to_element(video_name).click().perform()
    time.sleep(5)
    video_start = driver.find_element(By.XPATH, '//xg-start[@class="xgplayer-start"]')
    ActionChains(driver).move_to_element(video_start).click().perform()
    restart = driver.find_element(By.XPATH, '//xg-replay[@class="xgplayer-replay"]')
    while True:
        # 这个循环每秒钟判断是否播放完成
        time.sleep(1)
        if restart.value_of_css_property("display")!='none':
            break
# 打包 Pyinstaller -F video.py